package com.meichis.yndms.room.dao;

import androidx.room.*;

import com.meichis.yndms.model.VisitWork;

import java.util.List;

import io.reactivex.Flowable;

@Dao
public interface VisitWorkDao {
    @Query("SELECT * FROM VisitWork")
    Flowable<List<VisitWork>> findAll();

    @Query("SELECT * FROM VisitWork WHERE IsUpload=0 AND IsComplete=0 AND Client=:client AND SUBSTR(BeginTime,1,10)=:workTime")
    Flowable<VisitWork> find(int client, String workTime);

    @Query("SELECT * FROM VisitWork WHERE IsUpload=0 AND IsComplete=0 AND Client!=:noClient AND SUBSTR(BeginTime,1,10)=:workTime")
    VisitWork findNotInclude(int noClient, String workTime);

    @Query("SELECT * FROM VisitWork WHERE GUID=:guid")
    VisitWork find(String guid);

    @Query("SELECT * FROM VisitWork WHERE IsUpload=:isUpload and IsComplete=:isComplete")
    List<VisitWork> find(int isUpload, int isComplete);

    @Query("SELECT * FROM VisitWork WHERE IsUpload=:isUpload and IsComplete=:isComplete and Client=:client")
    VisitWork find(int client, int isUpload, int isComplete);

    @Query("DELETE FROM VisitWork")
    int deleteAll();

    @Query("DELETE FROM VisitWork WHERE GUID=:guid")
    int delete(String guid);

    @Query("DELETE FROM VisitWork WHERE IsUpload=:isUpload")
    int delete(int isUpload);

    @Delete
    int delete(VisitWork... VisitWorks);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    int update(VisitWork... VisitWorks);

    @Query("UPDATE VisitWork SET IsUpload=:newIsUpload WHERE IsUpload=:oldIsUpload")
    int update(int oldIsUpload, int newIsUpload);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long[] insertAll(List<VisitWork> list);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    long[] insert(VisitWork... VisitWorks);

    @Query("SELECT COUNT(*) FROM VisitWork WHERE IsUpload=:isUpload and IsComplete=:isComplete")
    int count(int isUpload, int isComplete);

    @Query("DELETE FROM VisitWork WHERE IsUpload= 1 AND IsComplete = 1")
    int deleteCompleteWork();
}
